﻿#pragma once

#include "../../Server/MirEnvir/Envir.h"
#include "../../Shared/Data/Stat.h"
#include <string>
#include <vector>
#include <stdexcept>
#include <any>
#include "exceptionhelper.h"
#include "../../stringhelper.h"
#include "../../tangible_filesystem.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;

namespace Server::Database
{
    class MonsterInfoFormNew : public System::Windows::Forms::Form
    {
    public:
        virtual ~MonsterInfoFormNew()
        {
            delete StatEnums;
            delete Table;
            delete components;
            delete monsterInfoGridView;
            delete panel1;
            delete panel2;
            delete groupView;
            delete rbtnViewAll;
            delete rbtnViewBasic;
            delete panel3;
            delete lblSearch;
            delete txtSearch;
            delete btnExport;
            delete btnImport;
            delete Modified;
            delete MonsterIndex;
            delete MonsterName;
            delete MonsterImage;
            delete MonsterAI;
            delete MonsterEffect;
            delete MonsterLevel;
            delete MonsterLight;
            delete MonsterAttackSpeed;
            delete MonsterMoveSpeed;
            delete MonsterViewRange;
            delete MonsterCoolEye;
            delete MonsterExperience;
            delete MonsterCanPush;
            delete MonsterAutoRev;
            delete MonsterUndead;
            delete MonsterCanTame;
        }

        Server::MirEnvir::Envir *getEnvir() const;

    private:
//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to the C# 'typeof' operator:
        Array *const StatEnums = Enum::GetValues(typeof(Stat));

        DataTable *Table;

    public:
        MonsterInfoFormNew();

    private:
        void InitializeItemInfoGridView();

    public:
        template<typename T>
        static DataTable *Enum2DataTable(bool sort = false)
        {
            DataTable *enumTable = new DataTable();
            DataColumn tempVar("Value", Enum::GetUnderlyingType(typeid(T)));
            enumTable->Columns->Add(&tempVar);
//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to the C# 'typeof' operator:
            DataColumn tempVar2("Display", typeof(std::string));
            enumTable->Columns->Add(&tempVar2);
            DataRow *EnumRow;

            auto values = Enum::GetValues(typeid(T))->Cast<T>();

            if (sort)
            {
                values = values->OrderBy([&] (std::any x)
                {
                    return x.ToString();
                });
            }

            for (auto e : *values)
            {
                EnumRow = enumTable->NewRow();
                EnumRow["Value"] = e;
//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to 'ToString':
                EnumRow["Display"] = e.ToString();
                enumTable->Rows->Add(EnumRow);
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete enumTable' statement was not added since enumTable was used in a 'return' or 'throw' statement.
            return enumTable;
        }

    private:
        void CreateDynamicColumns();

        void PopulateTable();

        void UpdateFilter();

        void SaveForm();

        DataRow *FindRowByMonsterName(const std::string &value);

        void monsterInfoGridView_CellValidating(std::any sender, DataGridViewCellValidatingEventArgs *e);

        void rbtnViewAll_CheckedChanged(std::any sender, EventArgs *e);

        void rbtnViewBasic_CheckedChanged(std::any sender, EventArgs *e);

        void txtSearch_KeyDown(std::any sender, KeyEventArgs *e);

        void btnImport_Click(std::any sender, EventArgs *e);

        void btnExport_Click(std::any sender, EventArgs *e);

        void monsterInfoGridView_DefaultValuesNeeded(std::any sender, DataGridViewRowEventArgs *e);

        void monsterInfoGridView_UserDeletingRow(std::any sender, DataGridViewRowCancelEventArgs *e);

        void monsterInfoFormNew_FormClosed(std::any sender, FormClosedEventArgs *e);

        void monsterInfoGridView_DataError(std::any sender, DataGridViewDataErrorEventArgs *e);


        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::IContainer *components = nullptr;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected:
        void Dispose(bool disposing) override;

        #pragma region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
    private:
        void InitializeComponent();

        #pragma endregion

        System::Windows::Forms::DataGridView *monsterInfoGridView;
        System::Windows::Forms::Panel *panel1;
        System::Windows::Forms::Panel *panel2;
        System::Windows::Forms::GroupBox *groupView;
        System::Windows::Forms::RadioButton *rbtnViewAll;
        System::Windows::Forms::RadioButton *rbtnViewBasic;
        System::Windows::Forms::Panel *panel3;
        System::Windows::Forms::Label *lblSearch;
        System::Windows::Forms::TextBox *txtSearch;
        System::Windows::Forms::Button *btnExport;
        System::Windows::Forms::Button *btnImport;
        System::Windows::Forms::DataGridViewCheckBoxColumn *Modified;
        System::Windows::Forms::DataGridViewTextBoxColumn *MonsterIndex;
        System::Windows::Forms::DataGridViewTextBoxColumn *MonsterName;
        System::Windows::Forms::DataGridViewComboBoxColumn *MonsterImage;
        System::Windows::Forms::DataGridViewTextBoxColumn *MonsterAI;
        System::Windows::Forms::DataGridViewTextBoxColumn *MonsterEffect;
        System::Windows::Forms::DataGridViewTextBoxColumn *MonsterLevel;
        System::Windows::Forms::DataGridViewTextBoxColumn *MonsterLight;
        System::Windows::Forms::DataGridViewTextBoxColumn *MonsterAttackSpeed;
        System::Windows::Forms::DataGridViewTextBoxColumn *MonsterMoveSpeed;
        System::Windows::Forms::DataGridViewTextBoxColumn *MonsterViewRange;
        System::Windows::Forms::DataGridViewTextBoxColumn *MonsterCoolEye;
        System::Windows::Forms::DataGridViewTextBoxColumn *MonsterExperience;
        System::Windows::Forms::DataGridViewCheckBoxColumn *MonsterCanPush;
        System::Windows::Forms::DataGridViewCheckBoxColumn *MonsterAutoRev;
        System::Windows::Forms::DataGridViewCheckBoxColumn *MonsterUndead;
        System::Windows::Forms::DataGridViewCheckBoxColumn *MonsterCanTame;
    };
}
